package com.yongyun.reptile.service;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Random;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.keix.common.utils.UuidUtils;
import com.yongyun.reptile.dao.ArticleDao;
import com.yongyun.reptile.utils.HttpClientUtils;
import com.yongyun.reptile.utils.StringUtils;

/** 
* @author liyongkang
* @version 创建时间：2018年9月11日 下午9:35:43
* 类说明 ：
*/

public class ReptileService {

	public static final String QB_URL = "https://www.qiushibaike.com";
	public static final String ARTICLE = "/article";
	public static String FIRST_NUMBER = "/";
	
	public static final String MYSQL_DRIVER = "com.mysql.jdbc.Driver";
	public static final String MYSQL_URL = "jdbc:mysql://localhost:3306/riyong";
	public static final String username = "root";
	public static final String password = "kang";
	
	{
		
		try {
			Class.forName(MYSQL_DRIVER);
			Random rand = new Random();
			int number = rand.nextInt(10000000)+ 110000000;
			FIRST_NUMBER = "/" + String.valueOf(number);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		ReptileService reptileService = new ReptileService();
		String nextUrl = ReptileService.QB_URL+ReptileService.ARTICLE+reptileService.FIRST_NUMBER;
		
		while(nextUrl!=null){
			nextUrl = reptileService.getHtml(nextUrl);
		}
	}
	
	/**
	 * 获取html
	 * @param url
	 * @return 返回下一个地址
	 */
	public String getHtml(String url){
		String result = HttpClientUtils.get(url, null);
		Document doc = Jsoup.parse(result);
		String content = doc.select(".content-text").html();
		String comment = doc.select(".main-text").html();
		//System.out.println(content);
		insertArticle(content, comment, url);
		String nextArt = doc.select("#articleNextLink").val();
		String nextUrl = null;
		if(StringUtils.isNotEmpty(nextArt)){
			nextUrl = QB_URL + nextArt;
		}
		if(StringUtils.isEmpty(nextArt)){
			System.out.println("没找到："+url);
			insertNoUrl(url);
			String[] urlSplit = url.split("/");
			String numberStr = urlSplit[urlSplit.length-1];
			int number = 0;
			if("www.qiushibaike.com".equals(numberStr)){
				Random rand = new Random();
				number = rand.nextInt(10000000)+ 110000000;
			}else{
				number = Integer.parseInt(numberStr);
			}
			nextUrl = QB_URL + ARTICLE + "/" + (number+1);
		}
		return nextUrl;
	}
	
	public void insertArticle(String content, String comment, String oraginUrl){
		String uuid = UuidUtils.uuid();
		Connection conn = null;
		try {
			conn = DriverManager.getConnection(MYSQL_URL, username, password);
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		try {
			ArticleDao.insertArticle(conn, uuid, content, comment, oraginUrl);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void insertNoUrl(String url){
		String uuid = UuidUtils.uuid();
		Connection conn = null;
		try {
			conn = DriverManager.getConnection(MYSQL_URL, username, password);
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		try {
			ArticleDao.insertNoUrl(conn, uuid, url);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
}
